<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Rudolph_tabelle extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->lang->load('breadcrumb', 'estonian');
        $this->load->model('Rudolph_model', 'RudolphModel');
    }

    public function index($pool = false, $stroke = false) {
        if ($stroke != 'free' && $stroke != 'fly' && $stroke != 'medley' && $stroke != 'back' && $stroke != 'breast') {
            redirect('rudolph_tabelle/all/free');
        }
        if ($pool != 'all' && $pool != 50) {
            redirect("rudolph_tabelle/all/$stroke");
        }
        $distances = array(
            'free' => array(
                '50' => 0,
                '100' => 1,
                '200' => 2,
                '400' => 3,
                '800' => 4,
                '1500' => 5),
            'back' => array(
                '50' => 0,
                '100' => 1,
                '200' => 2),
            'breast' => array(
                '50' => 0,
                '100' => 1,
                '200' => 2),
            'fly' => array(
                '50' => 0,
                '100' => 1,
                '200' => 2),
            'medley' => array(
                '200' => 0,
                '400' => 1));

        $values = $this->RudolphModel->fetch_rudolph_tabel($pool == 50 ? 50 : false, $stroke);
        $total = array();
        $count_values = count($values);
        $i = 1;
        $athlete_id = 0;
        $name_set = false;
        foreach ($values as $row) {
            if (($athlete_id != $row->id && $athlete_id != 0) || $i == $count_values) {
                $total[] = $result;
                $name_set = false;
                $result = array();
            }
            if (!$name_set) {
                $result['first_name'] = $row->first_name;
                $result['last_name'] = $row->last_name;
                $result['age'] = $row->age;
                $name_set = true;
            }
            $result[$distances[$row->stroke][$row->distance]] = array('result' => $this->Utils->ms_to_result_string($row->ms), 'points' => $row->rudolph);
            $i++;
            $athlete_id = $row->id;
        }
        $data['pool'] = $pool;
        $data['stroke'] = $stroke;
        $data['values'] = $total;
        $data['distances'] = $distances[$stroke];
        $this->load->template('rudolph', $data);
    }

}